//index.js
var utils = require('../../utils/util.js')
//获取应用实例
var app = getApp()
Page({
    data: {
        crystalImgUrl: '//',
        personCount: '?',
        joinCount: '?',
        completeCount: '?',
        userInfo: {}
    },
    //事件处理函数
    gotoExps: function (event) {
        wx.navigateTo({
            url: '/pages/exp/exp'
        })
    },
    onLoad: function () {
        const that = this;
        var res = wx.getStorageSync('statUpdateTime');
        var currentTime = (new Date()).getTime();
        if (res && (currentTime - parseInt(res.data) < 60000)) {
            console.log(currentTime, res.data)
            that.setData({
                personCount: wx.getStorageSync('userCount'),
                joinCount: wx.getStorageSync('attendCount'),
                completeCount: wx.getStorageSync('attendCompleteCount')
            })
        } else {
            utils.httpRequest({
                url: '/Stat',
                success: function (res) {
                    console.log(res.data)
                    that.setData({
                        personCount: res.data['userCount'],
                        joinCount: res.data['attendCount'],
                        completeCount: res.data['attendCompleteCount']
                    })
                    wx.setStorageSync('userCount', res.data['userCount'])
                    wx.setStorageSync('attendCount', res.data['attendCount'])
                    wx.setStorageSync('attendCompleteCount', res.data['attendCompleteCount'])
                    wx.setStorageSync('statUpdateTime', (new Date()).getTime())
                }
            })
        }


        if (Math.random() > 0.8) {
            utils.httpRequest({
                url: '/Index/randImg',
                success: function (res) {
                    console.log(res.data)
                    that.setData({
                        crystalImgUrl: res.data
                    })
                }
            })
        } else {
            this.setData({
                crystalImgUrl: '../../images/You_are_beautiful.jpg'
            })
        }
    },

    downloadAndViewDoubleTestPDF: function () {
        wx.getNetworkType({
            success: function (res) {
                // 返回网络类型, 有效值：
                // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
                var networkType = res.networkType
                //   if(networkType=='wifi'){
                //       utils.downloadAndOpen('https://cdn.water-crystal.org/pdf/doubleCN.pdf');
                //   }else 
                if (networkType == 'none') {

                } else {
                    wx.showModal({
                        title: '查看中文文献？',
                        content: '查看此文档需要消耗约0.5MB流量',
                        success: function (res) {
                            if (res.confirm) utils.downloadAndOpen('https://cdn.water-crystal.org/pdf/doubleCN.pdf');
                        }
                    })
                }
            }
        })

    },

    downloadAndViewTripleTestPDF: function(){
        wx.getNetworkType({
            success: function (res) {
                // 返回网络类型, 有效值：
                // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
                var networkType = res.networkType
                //   if(networkType=='wifi'){
                //       utils.downloadAndOpen('https://cdn.water-crystal.org/pdf/doubleCN.pdf');
                //   }else 
                if (networkType == 'none') {

                } else {
                    wx.showModal({
                        title: '查看中文文献？',
                        content: '查看此文档需要消耗约0.5MB流量',
                        success: function (res) {
                            if (res.confirm) utils.downloadAndOpen('https://cdn.water-crystal.org/pdf/tripleCN.pdf');
                        }
                    })
                }
            }
        })
    },

    downTripleEn: function(){
        wx.getNetworkType({
            success: function (res) {
                // 返回网络类型, 有效值：
                // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
                var networkType = res.networkType
                //   if (networkType == 'wifi') {
                //       utils.downloadAndOpen('https://cdn.water-crystal.org/pdf/doubleEN.pdf');
                //   } else 
                if (networkType == 'none') {

                } else {
                    wx.showModal({
                        title: '查看英文文献？',
                        content: '查看此文档需要消耗约0.2MB流量',
                        success: function (res) {
                            if (res.confirm) utils.downloadAndOpen('https://cdn.water-crystal.org/pdf/tripleEN.pdf');
                        }
                    })
                }
            }
        })
    },

    downDoubleEn: function () {
        wx.getNetworkType({
            success: function (res) {
                // 返回网络类型, 有效值：
                // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
                var networkType = res.networkType
                //   if (networkType == 'wifi') {
                //       utils.downloadAndOpen('https://cdn.water-crystal.org/pdf/doubleEN.pdf');
                //   } else 
                if (networkType == 'none') {

                } else {
                    wx.showModal({
                        title: '查看英文文献？',
                        content: '查看此文档需要消耗约0.2MB流量',
                        success: function (res) {
                            if (res.confirm) utils.downloadAndOpen('https://cdn.water-crystal.org/pdf/doubleEN.pdf');
                        }
                    })
                }
            }
        })
    },

    onShareAppMessage: function () {
        return { title: "意念能影响水吗？", imageUrl: '../../images/You_are_beautiful.jpg' }
    },
    onPullDownRefresh: function () { wx.stopPullDownRefresh() }
})
